package geekzpacho.examples.ehcache;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Main {
 
	
	// ------ SQL ----------
	//CREATE SCHEMA `geekpacho` ;
	//
	//CREATE TABLE `vehicles` (
    //	  `idVehicle` int(11) NOT NULL,
    //	  `name` varchar(45) DEFAULT NULL,
    //	  `description` varchar(150) DEFAULT NULL,
    //	  PRIMARY KEY (`idVehicle`)
    //	) 
		
//	INSERT INTO `vehicles` (`idVehicle`,`name`,`description`) VALUES (1,'Car','Motor vehicle small or medium size, used for carrying people and can accommodate no more than nine seats.');
//	INSERT INTO `vehicles` (`idVehicle`,`name`,`description`) VALUES (2,'MotorBike','Two-wheeled motor vehicle with one or two saddles and sometimes with sidecar.');
//	INSERT INTO `vehicles` (`idVehicle`,`name`,`description`) VALUES (3,'Truck','Four or more vehicle wheels which is used to transport heavy loads.');
	
	
	public static void main(String[] args) {
		
		 Long time;
		 
		 ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		 VehicleDAO dao = (VehicleDAO)context.getBean("vehicleDAO");
				 
		 System.out.println("*** SELECT ALL ***");	
		 time = System.nanoTime();
		 List<Vehicle> vehiclesNoCache = dao.getAll();
		 System.out.println("First call without cache: " + (System.nanoTime() - time) + " ms");
		 for (Vehicle objVehicle : vehiclesNoCache) {
			 System.out.println(objVehicle.toString());			 
		 }
		 
		 time = System.nanoTime();
		 List<Vehicle> vehiclesCache = dao.getAll();
		 System.out.println("Second call with cache: " + (System.nanoTime() - time) + " ns");
		 for (Vehicle objVehicle : vehiclesCache) {
			 System.out.println(objVehicle.toString());
		 }
		 
		 System.out.println("**************");
		 
		 System.out.println("*** SELECT BY ID ***");
		 time = System.nanoTime();
		 Vehicle vehicleNoCache = dao.getVehicle(1);
		 System.out.println("First call without cache: " + (System.nanoTime() - time) + " ns");
		 System.out.println(vehicleNoCache.toString());
		 
		 time = System.nanoTime();
		 Vehicle vehicleCache = dao.getVehicle(1);
		 System.out.println("Second call with cache: " + (System.nanoTime() - time) + " ns");
		 System.out.println(vehicleCache.toString());
		 
		 System.out.println("**************");
		
	}
}